﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using WebServerLib;
using System.Xml.Serialization;
using System.Globalization;
using DatabaseLib;
using SysLogLib;
using CommandLib;
using SettLib;

namespace DLinkControl
{
    public partial class MainFrm : Form
    {

        public cSettings Settings;

        cWebServer WebServer;
        cSysLog Syslog;
        cCommand CommandHandler;
        ConnectionConfig ConnConf;
        DB db;
        bool AppTerminate = false;

        public MainFrm()
        {
            InitializeComponent();
            Settings = new cSettings();
            if (!Settings.LoadSettings(Path.Combine(Application.StartupPath,"settings.xml")))
            {
                AppTerminate = true;
                Application.Exit();
            }
            if (!AppTerminate)
            {
                ConnConf = new ConnectionConfig(DatabaseTypes.typeMsSqlCE);
                ConnConf.SDFFilename = Settings.Data.Main.SDFFilename;
                db = new DB(ConnConf);
                WebServer = new cWebServer(Settings.Data.Webserver, db);
                //WebServer.ExecuteQuery += db.ExecuteQuery;
                //WebServer.GetQueryTable += db.GetQueryTable;
                CommandHandler = new cCommand(WebServer, Settings, db);
                WebServer.CommandHandler += CommandHandler.ExecuteCommand;
                WebServer.ErrorHandler += ErrorHandler;
                Syslog = new cSysLog(Settings.Data.Syslog, db);
                //Syslog.ExecuteQuery += db.ExecuteQuery;
                //Syslog.ExecuteQueryBatch += db.ExecuteQueryBatch;
                //Syslog.GetQueryTable += db.GetQueryTable;
                Syslog.ErrorHandler += ErrorHandler;
            }
        }

        private void ErrorHandler(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Syslog.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Syslog.Stop();
        }

        private void MainFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if ((WebServer != null) && (WebServer.Running))
                WebServer.Stop();
            if ((Syslog != null) && (Syslog.Running))
                Syslog.Stop();
            Settings.SaveSettings(Path.Combine(Application.StartupPath, "settings.xml"));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            WebServer.Start();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            WebServer.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Syslog != null)
            {
                label3.Text = Syslog.GetCurrentCacheNumber().ToString();
                label4.Text = Syslog.GetCurrentCacheSize().ToString();
                label6.Text = Syslog.GetQueueLength().ToString();
            }
            Application.DoEvents();

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            timer2.Enabled = checkBox1.Enabled;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (WebServer != null)
            {
                WebServer.LoginUser(textBox1.Text, textBox2.Text);
            }
        }

    }
}
